﻿using System.ComponentModel;
using System.Linq;
using System.Windows;
using Fayshine.VA.Helpers;
using Fayshine.VA.RIAService.Web;
using Fayshine.VA.Controls;
using System.Collections.Generic;

namespace Fayshine.VA.ViewModels
{
    public class DeviceCameraViewModel : INotifyPropertyChanged
    {
        #region Private Fields

        private FayshineDomainContext m_Context = null;

        #endregion

        public DeviceCameraViewModel()
        {
            this.Devices = new PagedObservableCollectionView<vaserver>();

            m_Context = new FayshineDomainContext();
        }

        #region Properties

        public PagedObservableCollectionView<vaserver> Devices
        {
            get;
            set;
        }

        #endregion

        #region Public Methods

        public void GetAllDevicess()
        {
            var getDevicesQuery = m_Context.GetVaserversQuery();
            var loadDevicesOperation = m_Context.Load(getDevicesQuery, (s) =>
            {
                if (!s.HasError)
                {
                    s.Entities.ToList().ForEach(e =>
                    {
                        this.Devices.Add(e);
                    });
                }
                else
                {
                    MessageBox.Show(s.Error.Message + s.Error.StackTrace);
                }
                BusyIndicator indicator = Application.Current.RootVisual as BusyIndicator;
                indicator.IsBusy = false;
            }, true);
        }

        public void AddDevice(vaserver vaserver)
        {
            this.Devices.Add(vaserver);
            this.m_Context.vaservers.Add(vaserver);
            SaveData();
        }

        public void Update(vaserver vaserver,List<camera> deletedCameras)
        {
            deletedCameras.each(d => {

                this.m_Context.cameras.Remove(d);
            });
            SaveData();
        }

        public void DeleteDevice(vaserver vaserver)
        {
            vaserver.cameras.each(e => {

                this.m_Context.cameras.Remove(e);
            
            });

            this.Devices.Remove(vaserver);
            this.m_Context.vaservers.Remove(vaserver);
            SaveData();
        }

        public void DeleteDevices(List<vaserver> devices)
        {
            devices.each(u =>
            {
                u.cameras.each(e =>
                {

                    this.m_Context.cameras.Remove(e);

                });
                this.Devices.Remove(u);
                this.m_Context.vaservers.Remove(u);
            });
            SaveData();
        }

        public void SaveData()
        {
            if (this.m_Context.HasChanges)
            {
                this.m_Context.SubmitChanges((s) =>
                {

                    //BusyIndicator indicator = Application.Current.RootVisual as BusyIndicator;

                    //indicator.IsBusy = false;
                    if (s.HasError)
                    {
                        MessageBox.Show("Error occurs,Save Failed!");
                    }

                }, null);
            }
        }

        public void SaveData2()
        {
            if (this.m_Context.HasChanges)
            {
                this.m_Context.SubmitChanges((s) =>
                {

                    //BusyIndicator indicator = Application.Current.RootVisual as BusyIndicator;

                    //indicator.IsBusy = false;
                    if (s.HasError)
                    {
                        MessageBox.Show("Error occurs,Save Failed!");
                    }
                    else
                    {
                        MessageBox.Show("保存成功！");
                    }

                }, null);
            }
        }

        public bool ExistDevice(string deviceName)
        {
            return this.Devices.Where(u => u.HostName.Equals(deviceName, System.StringComparison.OrdinalIgnoreCase)).Any();
        }

        #endregion


        #region INotifyPropertyChanged Interface

        public event PropertyChangedEventHandler PropertyChanged;
        public void NofityPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}
